
package horde.minefield.andres.robots;

import horde.interfaces.Requests;
import horde.patterns.Core;
import horde.patterns.Msg;
import horde.internalResources.Directory;
import horde.internalResources.HorrorControl.HorrorControl;
import java.util.ArrayList;

/**
 * @author Andres
 */
public class robot extends Core {
    private HorrorControl hc = HorrorControl.getInstance();
    private int workload = 0;
    private int receivedReports = 0;
    private ArrayList<Integer> reports;
    private int robotCount = 0;
    private Requests req;
    private robotControl rc;
    /* Control pairs
     * Request  Run
     * -----------------------------------------
     * start    login
     * logged   -
     * work     sendEvaluation
     * eval     processEval
     * winner   -
     * -        doWork
     */

    public void login () {
        HorrorControl.getInstance().trace(this.getID(), "Start -> Login", HorrorControl.TRACE_INFO);
        rc = (robotControl) requests.getSharedResource("robotControl");
        workload = getID().length();
        receivedReports = 0;
        reports = new ArrayList();
        rc.report(getID(), "loggin...0", "Workload " + robotCount);
        rc.login(super.getID(), "loggin...1", "Workload: " + workload);
    }

    public void sendEvaluation (Integer number_of_robots) {
        hc.trace(this.getID(), "Sending evaluation...", HorrorControl.TRACE_INFO);
        robotCount = number_of_robots - 1;
        receivedReports = 0;
        /* Please evaluate me */
        Msg msg = Msg.composeForGroup(getID(), "default", "eval", new Integer(workload));
        rc.report(getID(), "evaluating("+robotCount+")", "Sending my eval");
        req = Directory.getInstance().getRequests();
        req.post(msg);
    }

    public void processEval (Integer remoteWorkload) {
        receivedReports++;
        rc.report(getID(), "evaluating", "Got " + receivedReports + " evals out of " + robotCount);
        reports.add(remoteWorkload);
        if (receivedReports == robotCount) {
            boolean myTurn = true;
            for (int w : reports) {
                if (w < workload) myTurn = false;
            }
            if (myTurn) {
                Msg msg = Msg.composeRequest(getID(), getID(), "winner");
                req.post(msg);
            } else {
                rc.report(getID(), "restarting", "I lost");
                try {
                    Thread.sleep(2000);
                } catch (Exception e) {}
                Msg msg = Msg.composeRequest(getID(), getID(), "loser");
                req.post(msg);
            }
        }        
    }
    public void doWork () {
        rc.report(getID(), "working", "I can do the task");
        try {
            Thread.sleep(5000);
            Msg msg = Msg.composeRequest(getID(), getID(), "done");
            System.err.println (this.getID() + "work done");
            req.post(msg);
        } catch (Exception e) {}
    }

    public void init () {
        receivedReports = 0;
        reports = new ArrayList();
        rc.report(getID(), "ready", "Workload " + workload);
    }
}
